package Controller.Sign;


import Model.SignModel.DeviceArrangeHg;
import Model.Staff;
import Model.User;
import Service.DeviceArrangeHgService;
import Service.StaffService;
import Service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import utils.permission.ConstantsUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "/DeviceArrangeHg")
public class DeviceArrangeHgController {

    @Autowired
    private DeviceArrangeHgService deviceArrangeHgService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/getDeviceArrangeHgList")
    @ResponseBody
    public PageInfo getDeviceArrangeHgList(@RequestParam(value = "pn", defaultValue = "1") Integer pn, @RequestParam(value = "limit", defaultValue = "10") Integer limit, HttpServletRequest request, DeviceArrangeHg deviceArrangeHg){
        User user = (User) request.getSession().getAttribute(ConstantsUtils.SessionObj.CURRENT_USER);
        String Role_id=this.userService.findRoleIDbyUserNumber(user.getUser_number());
        if(Role_id.equals("2")){
            String settleCode =userService.findSettleCodebyUserNumber(user.getUser_number());
            System.out.println("找到机构代码："+settleCode);
            deviceArrangeHg.setOrganization_id(settleCode);
            PageHelper.startPage(pn, limit);
            List<DeviceArrangeHg> list = deviceArrangeHgService.getDeviceArrangeHgList(deviceArrangeHg);
            System.out.println("[获取到发放护工设备的信息：]"+list);
            PageInfo page = new PageInfo(list);
            return page;
        }else{
            PageHelper.startPage(pn, limit);
            List<DeviceArrangeHg> list = deviceArrangeHgService.getDeviceArrangeHgList(deviceArrangeHg);
            System.out.println("[获取到发放护工设备的信息：]"+list);
            PageInfo page = new PageInfo(list);
            return page;
        }
    }

    @RequestMapping(value = "/deleteDeviceArrangeHg")
    @ResponseBody
    public int deleteDeviceArrangeHg(int id){
        int status = 0;
        try {
            deviceArrangeHgService.deleteDeviceArrangeHg(id);
            System.out.println("[删除发放护工设备完成~]");
        }catch (Exception e){
            status = -1;
            e.printStackTrace();
        }
        return status;
    }

    @RequestMapping(value = "/updateDeviceArrangeHg")
    @ResponseBody
    public int updateDeviceArrangeHg(DeviceArrangeHg deviceArrangeHg){
        int status = 0;
        try {
            deviceArrangeHgService.updateDeviceArrangeHg(deviceArrangeHg);
            System.out.println("[更新发放护工设备完成~]");
        }catch (Exception e){
            status = -1;
            e.printStackTrace();
        }
        return status;
    }

    @RequestMapping(value = "/addDeviceArrangeHg")
    @ResponseBody
    public int addDeviceArrangeHg(HttpServletRequest request, DeviceArrangeHg deviceArrangeHg, Staff staff){
        int status = 0;
        User user = (User) request.getSession().getAttribute(ConstantsUtils.SessionObj.CURRENT_USER);
        String settleCode =userService.findSettleCodebyUserNumber(user.getUser_number());
        System.out.println("当前机构ID为："+settleCode);
        deviceArrangeHg.setOrganization_id(settleCode);
        String organization = user.getUser_name();
        System.out.println("当前机构为："+organization);
        deviceArrangeHg.setOrganization(organization);

        staff.setId_number(request.getParameter("hg_id_number"));
        try {
            deviceArrangeHgService.addDeviceArrangeHg(deviceArrangeHg);
            System.out.println("[添加发放护工设备完成~]");
            staffService.updateHgDevice(staff);
            System.out.println("[同步发放护工设备完成~]");
        }catch (Exception e){
            status = -1;
            e.printStackTrace();
        }
        return status;
    }
}
